package pl.th.permission.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import java.util.Collection;
import java.util.List;

import pl.th.permission.R;

public class CustomDialog extends Dialog implements View.OnClickListener {

    private String title;

    private String headerMessage;

    private String footerMessage;

    private List<String> permissions;

    private DialogInterface.OnClickListener mPositiveListener;
    private DialogInterface.OnClickListener mNegativeListener;

    public CustomDialog(Context context) {
        super(context, R.style.PermissionMentionDialog);
//        setOnCancelListener(dialog -> {
//            if (mNegativeListener != null) {
//                mNegativeListener.onClick(dialog, R.id.pl_th_permission_dialog_negative_button);
//            }
//        });
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setTitle(@StringRes int resId) {
        setTitle(getContext().getString(resId));
    }

    public void setHeaderMessage(String message) {
        this.headerMessage = message;
    }

    public void setHeaderMessage(@StringRes int resId) {
        setHeaderMessage(getContext().getString(resId));
    }

    public void setFooterMessage(String footerMessage) {
        this.footerMessage = footerMessage;
    }

    public void setFooterMessage(@StringRes int resId) {
        setFooterMessage(getContext().getString(resId));
    }

    public void setPermissions(List<String> permissions) {
        this.permissions = permissions;
    }

    public void setPositiveListener(OnClickListener positiveListener) {
        this.mPositiveListener = positiveListener;
    }

    public void setNegativeListener(OnClickListener negativeListener) {
        this.mNegativeListener = negativeListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wlp.y = getContext().getResources()
                .getDimensionPixelSize(R.dimen.pl_th_permission_dialog_border_margin);
        window.setAttributes(wlp);
        setContentView(R.layout.pl_th_permission_dialog);
        findViewById(R.id.pl_th_permission_dialog_positive_button).setOnClickListener(this);
        findViewById(R.id.pl_th_permission_dialog_negative_button).setOnClickListener(this);
        ListView listView = findViewById(R.id.pl_th_permission_dialog_list);
        listView.setAdapter(new Adaptor());
        if (!TextUtils.isEmpty(headerMessage)) {
            addHeader(listView);
        }
        if (!TextUtils.isEmpty(footerMessage)) {
            addFooter(listView);
        }
        TextView titleView = findViewById(R.id.pl_th_permission_dialog_title);
        titleView.setText(title);
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        if (id == R.id.pl_th_permission_dialog_positive_button) {
            if (mPositiveListener != null) {
                mPositiveListener.onClick(this, R.id.pl_th_permission_dialog_positive_button);
            }
        } else if (id == R.id.pl_th_permission_dialog_negative_button) {
            if (mNegativeListener != null) {
                mNegativeListener.onClick(this, R.id.pl_th_permission_dialog_negative_button);
            }
        }
    }

    private void addHeader(ListView listView) {
        if(TextUtils.isEmpty(headerMessage)) return;
        TextView headerView = createTextView();
        final int paddingSize = getContext().getResources()
                .getDimensionPixelSize(R.dimen.pl_th_permission_dp8);
        headerView.setPadding(0, 0, 0, paddingSize);
        headerView.setText(headerMessage);
        headerView.setTextSize(16);
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
        );
        headerView.setLayoutParams(lp);
        listView.addHeaderView(headerView);
    }

    private void addFooter(ListView listView) {
        if(TextUtils.isEmpty(footerMessage)) return;
        TextView footerView = createTextView();
        final int paddingSize = getContext().getResources()
                .getDimensionPixelSize(R.dimen.pl_th_permission_dp8);
        footerView.setPadding(0, paddingSize, 0, 0);
        footerView.setText(footerMessage);
        footerView.setTextSize(16);
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
        );
        footerView.setLayoutParams(lp);
        listView.addFooterView(footerView);
    }

    private TextView createTextView() {
        TextView textView = new TextView(getContext());
        textView.setTextSize(15);
        final int textColor = getContext().getResources()
                .getColor(R.color.pl_th_permission_dialog_message_color);
        textView.setTextColor(textColor);
        textView.setGravity(Gravity.START);
        return textView;
    }

    private class Adaptor extends BaseAdapter {

        @Override
        public int getCount() {
            return permissions.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return permissions.get(position);
        }

        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                TextView textView = createTextView();
                final int paddingSize = parent.getResources().getDimensionPixelSize(R.dimen.pl_th_permission_dp2);
                textView.setPadding(0, paddingSize, 0, paddingSize);
                ViewGroup.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
                );
                textView.setTextColor(ContextCompat.getColor(getContext(), R.color.pl_th_permission_dialog_title_color));
                textView.setLayoutParams(lp);
                convertView = textView;
            }
            final String text = (position + 1) + ". " + getItem(position);
            ((TextView)convertView).setText(text);
            return convertView;
        }
    }


}
